//
//  Item.cpp
//  derleyici
//
//  Created by Avea on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#include "Item.h"

Item::Item(Rule *aRule, int dotLocation){
    dotIndex = dotLocation;
    for(int i=0; i<aRule->getSymbols().size(); i++){
        addSymbol(aRule->getSymbols()[i]);
    }
    nonterminal = aRule->getNonterminal();
    number = aRule->getNumber();
    
    if(dotIndex>=symbols.size())
        dotAtEnd = true;
    else
        dotAtEnd = false;
    
    //To-Do//
    /** next symboller nasıl belirlenecek **/
}

Item::Item(Rule *aRule, int dotLocation, Symbol *aSymbol){
    Item(aRule, dotLocation);
    addSymbolToNextSymbols(aSymbol);
}

bool Item::isDotAtEnd(){
    return dotAtEnd;
}


int Item::getDotLocation(){
    return dotIndex;
}

void Item::addSymbolToNextSymbols(Symbol *aSymbol){
    if(!hasNextSymbol(aSymbol))
        nextSymbols.push_back(aSymbol);
}
vector<Symbol *> Item::getNextSymbols(){
    return nextSymbols;
}

Symbol* Item::getNextSymbolFromDot(){
    return symbols[dotIndex];
}

void Item::printSymbols(){
    cout<<nonterminal->getIdentifier()<<" -> ";
    for(int i=0; i<symbols.size(); i++){
        if(i==dotIndex){
            cout<<". ";
        }
        cout<<symbols[i]->getIdentifier()<<" ";
    }
    if (dotIndex == symbols.size()) {
        cout << ". ";
    }
    for(int i=0; i<nextSymbols.size(); i++){
        cout<<", "<<nextSymbols[i]->getIdentifier();
    }
    cout<<endl;
}

bool Item::operator==(Item *aItem){
    bool result = true;
    //cout<<symbols.size() <<" = " <<aItem->getSymbols().size()<<" --- "<<dotIndex << " = " << aItem->getDotLocation() << " --- "<< nextSymbols.size() <<" = " << aItem->getNextSymbols().size() <<endl;
    if((symbols.size() == aItem->getSymbols().size()) && (dotIndex == aItem->getDotLocation())){
        for(int i=0; i<symbols.size(); i++){
            if(symbols[i]->getIdentifier().compare(aItem->getSymbols()[i]->getIdentifier())!=0){
                result = false;
                //cout<<symbols[i]->getIdentifier()<<" esit degil " << aItem->getSymbols()[i]->getIdentifier();
                break;
            }
        }
        /** sonraki sözcükleri karşılaştırılıyor **/
        if(result){
            //for(int i=0; i<nextSymbols.size(); i++){
            //    if(nextSymbols[i]->getIdentifier().compare(aItem->getNextSymbols()[i]->getIdentifier())!=0){
            //        result = false;
            //        break;
            //    }
            //}
            //cout<<"Gelen:";
            //printSymbols();
            for (int i=0; i<nextSymbols.size(); i++) {
                aItem->addSymbolToNextSymbols(nextSymbols[i]);
                //cout<<nextSymbols[i]->getIdentifier()<<" eklendi"<<endl;
            }
        }        
    }else{
        result = false;
    }
    
    return result;
}


bool Item::hasNextSymbol(Symbol *aSymbol){
    bool founded = false;
    for(int i=0; i<nextSymbols.size(); i++){
        if(nextSymbols[i]->getIdentifier().compare(aSymbol->getIdentifier())==0){
            founded = true;
            break;
        }
    }
    return founded;
}
